package com.project.controller;

import com.project.bean.CartBean;
import com.project.bean.UserBean;
import com.project.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("cart")
public class CartController {
    @Autowired
    private ICartService cartService;

    @RequestMapping("addCart")
    public String addCart(Integer productId, HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        cartService.addCart(productId, user.getId());
        return "ok";
    }

    @RequestMapping("del")
    public String del(Integer cartId) {
        cartService.del(cartId);
        return "ok";
    }

    @RequestMapping("updateNum")
    public String updateNum(Integer cartId, Integer num) {
        cartService.updateNum(cartId, num);
        return "ok";
    }

    @RequestMapping("findByUserId")
    public List<CartBean> findByUserId(HttpServletRequest req) {
        UserBean user = (UserBean) req.getSession().getAttribute("loginUser");
        return cartService.findByUserId(user.getId());
    }


}
